import numpy as np

from util.util_ris_pattern import point_2_phase, phase_2_bit, phaseBit_2_deg, \
    phase_2_pattern_xyz_fft, phase_2_pattern, eps




# phaseBit计算方向图
def phaseBits_2_patterns(phaseBits, bit_num):
    patterns = []
    for phaseBit in phaseBits:
        # fft计算方向图
        phaseDeg = phaseBit_2_deg(phaseBit, bit_num)
        phaseRad = np.deg2rad(phaseDeg)
        # pattern = phase_2_pattern(phaseRad)                # 公式法直接计算, 准确但速度太慢
        pattern, x, y = phase_2_pattern_xyz_fft(phaseRad)  # FFT法计算, 快速
        patterns.append(pattern)
    return patterns


# 轨迹计算方向图: traces算phaseBit
def traces_2_phaseBits(traces, bit_num):
    phaseBits = []
    for trace in traces:
        theta0 = trace[0]
        phi0 = trace[1]
        phaseRad = point_2_phase(theta0, phi0)
        phaseDeg = np.rad2deg(phaseRad)
        phaseBit, phaseDeg = phase_2_bit(phaseDeg, bit_num)
        phaseBits.append(phaseBit)
    return phaseBits


# 轨迹计算方向图: traces算phase
def traces_2_phaseRads(traces):
    phaseRads = []
    for trace in traces:
        theta0 = trace[0]
        phi0 = trace[1]
        phaseRad = point_2_phase(theta0, phi0)
        phaseRad = np.rad2deg(phaseRad)
        phaseRads.append(phaseRad)
    return phaseRads
